$(document).ready(function(){

    setNowAll();

    $("#fy").change(function(){
        var selectedYear = $("#fy").val();
        if (selectedYear == new Date().getFullYear()) {
            setNowAll();
            // console.log("2014");
        }else{
            $("#ty, #fm, #tm, #fd, #td").html("");
            for(var month = 1; month <= 12; month ++){
                var m = document.createElement("option");
                m.value = month;
                m.innerHTML = month;
                $("#fm").append(m);
            }
            for(var day = 1; day <= 31; day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#fd").append(d);
            }
            setToAll();
            // console.log("2015");
        }
    });

    $("#fm").change(function(){
        var selectedYear = $("#fy").val();
        var selectedMonth = $("#fm").val();
        $("#fd, #td").html("");
        if (selectedMonth == new Date().getMonth() + 1 && selectedYear == new Date().getFullYear()) {
            for(var day = new Date().getDate(); day <= getDaysInThisMonth(); day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#fd").append(d);
            }
        }else{
            for(var day = 1; day <= getDaysInMonth(parseInt(selectedMonth), parseInt(selectedYear)); day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#fd").append(d);
            }
        }

        setToAll();
    });

    $("#fd").change(setToAll);

    $("#ty").change(function(){
        var selectedYear = parseInt($("#ty").val());
        if ($("#fy").val() == new Date().getFullYear()) {
            if (selectedYear == $("#fy").val()) {
                setToAll()
            }else{
                $("#tm, #td").html("");
                for(var month = 1; month <= 12; month ++){
                    var m = document.createElement("option");
                    m.value = month;
                    m.innerHTML = month;
                    $("#tm").append(m);
                }
                for(var day = 1; day <= 31; day ++){
                    var d = document.createElement("option");
                    d.value = day;
                    d.innerHTML = day;
                    $("#td").append(d);
                }
            }
        }else{
            $("#tm, #td").html("");
            for(var month = 1; month <= 12; month ++){
                var m = document.createElement("option");
                m.value = month;
                m.innerHTML = month;
                $("#tm").append(m);
            }
            for(var day = 1; day <= 31; day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#td").append(d);
            }
        }
    })

    $("#tm").change(function(){
        var selectedYear = parseInt($("#ty").val());
        var selectedMonth = parseInt($("#tm").val());

        $("#td").html("");
        if ($("#fy").val() == selectedYear && $("#fm").val() == selectedMonth) {
            for(var day = parseInt($("#fd").val()); day <= getDaysInMonth(selectedMonth, selectedYear); day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#td").append(d);
            }
        }else{
            for(var day = 1; day <= getDaysInMonth(selectedMonth, selectedYear); day ++){
                var d = document.createElement("option");
                d.value = day;
                d.innerHTML = day;
                $("#td").append(d);
            }
        }
    })

    $("#number1,#number2").change(function(){
        var number1 = $("#number1").val();
        var number2 = $("#number2").val();
        if (parseInt(number1) > parseInt(number2)) {
            $("#sum_member_msg").text("");
            $("#button").removeAttr("disabled");
            $("#flag").css("display", "");
        }else if(parseInt(number1) < parseInt(number2)){
            $("#sum_member_msg").text("不能申请过多的座位！");
            $("#button").attr("disabled", "disabled");
            $("#flag").css("display", "none");
        }else{
            $("#sum_member_msg").text("");
            $("#button").removeAttr("disabled");
            $("#flag").css("display", "none");
        }
    });
    
    $("#button").click(function(){
        if ($("#act").val() === "" ||
            $("#lea").val() === "" ||
            $("#num").val() === "" ||
            $("#number1").val() === "" ||
            $("#member").val() === "") {
            alert("请填写完整信息!");
        }else{
            if (parseInt($("number1").val()) > parseInt($("#number2").val()) && $("#othername").val() === "") {
                alert("请填写完整信息!");
                $("#form1").attr("action", "");
            }else{
                $("#form1").attr("action", "../php/apply.php");
            }
        }
    })
});

function getDaysInMonth (month, year) {
    return new Date(year, month, 0).getDate();
}

function getDaysInThisMonth () {
    var d = new Date();
    return getDaysInMonth(d.getMonth() + 1, d.getFullYear());
}

function setNowAll () {
    var dateTime = new Date();
    var year = dateTime.getFullYear();

    $("#fy, #fm, #fd, #ty, #tm, #td").html("");
    //设置年份 
    for (var i = 0; i < 2; i++) {
        var y = document.createElement("option");
        y.value = year + i;
        y.innerHTML = year + i;
        $("#fy,#ty").append(y);
    }
    //设置月份
    for(var month = dateTime.getMonth() + 1; month <= 12; month ++){
        var m = document.createElement("option");
        m.value = month;
        m.innerHTML = month;
        $("#fm,#tm").append(m);
    }
    //设置日期
    for(var day = dateTime.getDate(); day <= getDaysInThisMonth(); day ++){
        var d = document.createElement("option");
        d.value = day;
        d.innerHTML = day;
        $("#fd, #td").append(d);
    }
}

function setToAll () {
    var year = parseInt($("#fy").val());
    var month = parseInt($("#fm").val());
    var day = parseInt($("#fd").val());
            
    $("#ty, #tm, #td").html("");
    if (year === new Date().getFullYear()) {
        for (var i = 0; i < 2; i++) {
            var y = document.createElement("option");
            y.value = year + i;
            y.innerHTML = year + i;
            $("#ty").append(y);
        }
    }else{
        var y = document.createElement("option");
        y.value = year;
        y.innerHTML = year;
        $("#ty").append(y);
    }

    for(var i = month; i <= 12; i ++){
        var m = document.createElement("option");
        m.value = i;
        m.innerHTML = i;
        $("#tm").append(m);
    }

    for(var i = day; i <= getDaysInMonth(month, year); i ++){
        var d = document.createElement("option");
        d.value = i;
        d.innerHTML = i;
        $("#td").append(d);
    }
}
